﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace com.sharepointutils.application2007
{
    public class SPWebServicesRequest
    {
        public String URL { get; set; }
        public String SOAPAction { get; set; }
        public String Username { get; set; }
        public String Password { get; set; }
        public String Domain { get; set; }
        public String Content { get; set; }
        public String Reponse { get; set; }
        public String Error { get; set; }
        public String Headers { get; set; }
        public String Status { get; set; }

        public SPWebServicesRequest (String url, String soapAction, String content)
        {
            URL = url;
            SOAPAction = soapAction;
            Content = content;
        }

        public Boolean Run( )
        {
            Boolean succeeded = false;
            try
            {
                Uri requestURI = new Uri(URL);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURI);
                request.Accept = "text/xml; charset=utf-8";
                request.ContentType = "text/xml; charset=utf-8";
                request.Headers.Add("SOAPAction", "\"" + SOAPAction + "\"");
                if (!String.IsNullOrEmpty(Username) && !String.IsNullOrEmpty(Password) && !String.IsNullOrEmpty(Domain))
                {
                    request.Credentials = new NetworkCredential(Username, Password, Domain);
                }
                
                request.Method = "POST";                
                request.ContentLength = Encoding.UTF8.GetByteCount(Content);
                
                using (Stream writer = request.GetRequestStream())
                {
                    writer.Write(Encoding.UTF8.GetBytes(Content), 0, Encoding.UTF8.GetByteCount(Content));
                }
                
                using (WebResponse response = request.GetResponse())
                {
                    StreamReader sr =
                            new StreamReader(
                                response.GetResponseStream());
                    String result = sr.ReadToEnd();
                    Headers = response.Headers.ToString();
                    Reponse = result;
                    Status = "";
                }
                succeeded = true;
            }
            catch (WebException ex)
            {
                StreamReader sr = new StreamReader(ex.Response.GetResponseStream());
                Reponse = sr.ReadToEnd();
                Headers = ex.Response.Headers.ToString();
                Status = ex.Status.ToString();
                Error = ex.Message;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
            }
            return succeeded;
        }

    }
}
